home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 1995 #1
/
Amiga Plus 1995 #1.iso
/
fish-disketten
/
fish_691-700
/
d695
/
icalc
/
scripts
/
root.ic
< prev
next >
Wrap
Text File
|
1994-12-13
|
559b
|
27 lines
# Newton-Raphson method for root-finding.
#
# example:
# > func f(x) = exp(x) - E # has root at x = 1
# > nroot(f(x), exp(x), 1.5, 0.01) # needs derivative
#
ROOT_MAXITER = 30
# Newton-Raphson method
func nroot(~xexpr,~dxexpr, x1, x2, xacc) = {
local j, df, dx, f, rtn
rtn = 0.5*(x1+x2)
for (j = 1; j <= ROOT_MAXITER; j+=1) {
x = rtn
f = xexpr # eval fn at rtn,
df = dxexpr # and derivative
dx = f/df
rtn -= dx
if ((x1-rtn)*(rtn-x2) < 0)
error(0) # farther away
if (abs(dx) <= xacc) return rtn
}
error(1) # max iterations reached
}